/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package jtyrian.screen;

/**
 *
 * @author thirdy.derivera@gmail.com
 */
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.Texture.TextureFilter;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.Action;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.utils.Align;
import com.badlogic.gdx.utils.Scaling;
import jtyrian.Tyrian;

import static com.badlogic.gdx.scenes.scene2d.actions.Actions.*;
import com.badlogic.gdx.scenes.scene2d.utils.Drawable;
import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable;

public class SplashScreen
        extends AbstractScreen {

    private Texture splashTexture;

    public SplashScreen(
            Tyrian game) {
        super(game);
    }

    @Override
    public void show() {
        super.show();

        // load the splash image and create the texture region
        splashTexture = new Texture("splash.png");

        // we set the linear texture filter to improve the stretching
        splashTexture.setFilter(TextureFilter.Linear, TextureFilter.Linear);

    }

    @Override
    public void resize(
            int width,
            int height) {
        super.resize(width, height);

        // let's make sure the stage is clear
        stage.clear();

        // in the image atlas, our splash image begins at (0,0) of the
        // upper-left corner and has a dimension of 512x301
        TextureRegion splashRegion = new TextureRegion(splashTexture, 0, 0, 512, 301);
        Drawable  drawableRegion = new TextureRegionDrawable(splashRegion);

        // here we create the splash image actor and set its size
        Image splashImage = new Image(drawableRegion, Scaling.stretch, Align.bottom | Align.left);
        splashImage.setWidth(width);
        splashImage.setHeight(height);

        // this is needed for the fade-in effect to work correctly; we're just
        // making the image completely transparent
        splashImage.getColor().a = 0f;

        // configure the fade-in/out effect on the splash image
        splashImage.addAction(sequence(fadeIn(0.75f), delay(1.75f), fadeOut(0.75f), new Action() {
            public boolean act(float delta) {
                game.setScreen(new MenuScreen(game));
                return true; // returning true consumes the event
            }
        }));

        // and finally we add the actors to the stage, on the correct order
        stage.addActor(splashImage);
    }

    @Override
    public void dispose() {
        super.dispose();
        splashTexture.dispose();
    }
}